package com.atguigu.petstore.app.controller;

import com.atguigu.petstore.app.service.LoginService;
import com.atguigu.petstore.app.service.SmsService;
import com.atguigu.petstore.app.vo.LoginParamVo;
import com.atguigu.petstore.app.vo.UserInfoVo;
import com.atguigu.petstore.common.result.Result;
import com.atguigu.petstore.member.entity.MemberInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author lfy
 * @Description
 * @create 2023-09-27 11:35
 */
@Tag(name = "登录系统")
@RequestMapping("/app")
@RestController
public class MemberLoginController {

    @Autowired
    SmsService smsService;

    @Autowired
    LoginService loginService;

    @Operation(summary = "获取手机验证码")
    @GetMapping("/login/getCode")
    public Result getCode(@RequestParam("phone") String phone){

        //发送验证码
        smsService.sendCode(phone);

        return Result.ok();
    }



    @Operation(summary = "登录")
    @PostMapping("/login")
    public Result login(@RequestBody LoginParamVo param){

        //登录业务，返回登录后的令牌
        String token = loginService.login(param);

        return Result.ok(token);
    }

    @Operation(summary = "获取用户信息")
    @GetMapping("/userInfo")
    public Result userInfo(@RequestHeader("token") String token){


        //获取redis中的用户信息
        MemberInfo memberInfo = loginService.getUserInfo(token);


        UserInfoVo infoVo = new UserInfoVo();
        infoVo.setAvatarUrl(memberInfo.getHeaderImg());
        infoVo.setNickname(memberInfo.getNickName());

        return Result.ok(infoVo);
    }
}
